package edu.indiana.cs.b534.torrent.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.indiana.cs.b534.torrent.context.TorrentContext;

public class TestDataCollector {
	
	public TestDataCollector(){
		Logger log = Logger.getLogger(TorrentManager.TORRENT_MANAGER_NAME);
        log.setLevel(Level.OFF);
	}

	public void runTest(int peerCount,Properties base,int batch){
		int port = 6570;

        try {
            Properties seedProperties = new Properties();
            seedProperties.putAll(base);
            
            seedProperties.setProperty("is-seed", "true");
            seedProperties.setProperty("seed-file", "resources/freeculture.Team_6.pdf");
            seedProperties.setProperty("port", String.valueOf(port));
            seedProperties.setProperty("peer-id", "-ERAN-SRINATH-SEED-1");

            TorrentContext seedContext = new TorrentContext(seedProperties);
            if(batch == 0){
                TorrentManager seed = new TorrentManager(seedContext);
                Thread seedThred = new Thread(seed);
                seedThred.start();
            }
            Thread.sleep(2000);
            TorrentManager[] peers = new TorrentManager[peerCount];
            for (int i = 0; i < peerCount; i++) {
                File f = new File("downloaded-torrents/" + i + "/");
                f.mkdirs();
                f.deleteOnExit();
                Properties peerProperties = new Properties();
                peerProperties.putAll(base);
                peerProperties.setProperty("is-seed", "false");
                peerProperties.setProperty("destination-folder", f.getAbsolutePath());
                peerProperties.setProperty("port", String.valueOf(port + i + 1));
                peerProperties.setProperty("peer-id", "-ERAN-SRINATH-PEER"+batch + i);

                TorrentContext peerContext = new TorrentContext(peerProperties);
                peers[i] = new TorrentManager(peerContext);
                Thread peerThread = new Thread(peers[i]);
                peerThread.start();
                Thread.sleep(2000);

            }
            while (true) {
                boolean completed = true;
                for (TorrentManager peer : peers) {
                    if (!peer.isCompleted()) {
                        completed = false;
                    }
                }
                if (completed) {
                    break;
                }
            }
            for (TorrentManager peer : peers) {
            	peer.shutDown();
            }
            
            System.out.println("Test completed");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		try {
			int batch;
			String basePropertiesFile;
			int peerCount = 4;
			
			if(args.length == 2 || args.length == 3){
				batch = Integer.parseInt(args[0]);
				basePropertiesFile = args[1];
				if(args.length == 3){
					peerCount = Integer.parseInt(args[3]);
				}
				Properties base = new Properties();
				FileInputStream in = new FileInputStream(basePropertiesFile);
				base.load(in);
				in.close();
				TestDataCollector dataCollector = new TestDataCollector();
				dataCollector.runTest(peerCount, base, batch);
			}else{
				System.out.println("batch-no base-prop-file [peer-count]");
			}
		} catch (Exception e) {
			System.out.println("batch-no base-prop-file [peer-count]");
		} 
		
	}
	
}
